//
//  XXBaseTableViewController.swift
//  APP
//
//  Created by Rdxer on 2017/12/6.
//  Copyright © 2017年 Rdxer. All rights reserved.
//

import UIKit
import Async
import DZNEmptyDataSet



open class XXBaseBaseTableViewController:UITableViewController {
    deinit {
        XXLoger.w(self.classForCoder.description() + "已释放" )
    }
    
    open override  func viewDidLoad() {
        super.viewDidLoad()
        
        if #available(iOS 15.0, *) {
            
            tableView.sectionHeaderTopPadding = 0
            
        } else {
            
            // Fallback on earlier versions
        };
        
        Async.main {
            XXLoger.w("已创建" + self.classForCoder.description() )
        }
        
        prepareTableView()
        
    }
    
    open func prepareTableView(){
        tableView.backgroundColor = FastComponentStyle.style.backgroundColor
    }
    
    override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated) 
    }
    
    override open func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
}

open class XXBaseTableViewController: XXBaseBaseTableViewController, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate {
    
    
    open override  func viewDidLoad() {
        super.viewDidLoad()
        
//        prepareEmptyDataSetView()
        
    }
    
    open override func prepareTableView(){
        super.prepareTableView()
        
        tableView.tableFooterView = UIView.init()
        tableView.estimatedRowHeight = 44
    }
    
    
    
    var emptyState:XXEmptyState = .loading{
        didSet{
            tableView.reloadEmptyDataSet()
        }
    }
    
    open func prepareEmptyDataSetView(){
        tableView.emptyDataSetSource = self
        tableView.emptyDataSetDelegate = self
    }
    
    public func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
        return NSAttributedString.init(string: emptyState.desc)
    }
    
    public func image(forEmptyDataSet scrollView: UIScrollView) -> UIImage? {
        return emptyState.image
    }
    
    public func emptyDataSet(_ scrollView: UIScrollView, didTap view: UIView) {
        emptyState = .requestFailed(msg: "请求失败..".i18n)
    }
    
    public func emptyDataSetShouldAnimateImageView(_ scrollView: UIScrollView) -> Bool {
        return emptyState.animat
    }
    
    public func imageAnimation(forEmptyDataSet scrollView: UIScrollView) -> CAAnimation? {
        return CABasicAnimation.init(keyPath: "transform").then({
            $0.fromValue = NSValue.init(caTransform3D: CATransform3DIdentity)
            $0.toValue = NSValue.init(caTransform3D: CATransform3DMakeRotation(.pi / 2, 0, 0, 1))
            $0.duration = 0.25
            $0.isRemovedOnCompletion = false
            $0.isCumulative = true
            $0.repeatCount = Float.greatestFiniteMagnitude
            
        })
    }
}




// MARK: -
// MARK: - prepareEmptyDataSetView

public enum XXEmptyState {
    /// 加载中
    case loading
    /// 无数据
    case empty
    /// 无网络
    case noNetwork
    /// 请求失败
    case requestFailed(msg:String?)
}

extension XXEmptyState{
    
    
    
    var desc:String{
        switch self {
        case .empty:
//            return ""
            return "无数据".i18n
        case .loading:
            return "加载中".i18n
        case .noNetwork:
            return "无网络".i18n
        case .requestFailed(let msg):
                return msg?.nilOrNotEmptyString ?? "请求错误".i18n
        }
    }
    
    var image:UIImage{
        switch self {
        case .empty:
                return "XX_EmptyPage_icon_empty".xx_toImage(from: XXBaseTabBarController.self)
        case .loading:
                return "XX_EmptyPage_icon_loading".xx_toImage(from: XXBaseTabBarController.self)
        case .noNetwork:
                return "XX_EmptyPage_icon_no_network".xx_toImage(from: XXBaseTabBarController.self)
        case .requestFailed:
                return "XX_EmptyPage_icon_request_failed".xx_toImage(from: XXBaseTabBarController.self)
        }
    }
    
    var animat:Bool{
        switch self {
        case .loading:
            return true
        default: return false
        }
    }
    
}
